package org.yxing.palpalgps.utils;

import java.io.IOException;
import java.util.List;

import org.apache.http.NoHttpResponseException;
import org.apache.http.message.BasicNameValuePair;
import org.yxing.palpalgps.oauth.OAuth;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class LocationTools {
    private static final String TAG = LocationTools.class.getSimpleName();
    private static final String UPDATE_URL = "user/location";
    
    LocationManager mLocationManager;
    
    private String provider;
    
    public LocationTools(LocationManager locationManager) {
        mLocationManager = locationManager;
        
        //set Criteria
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setSpeedRequired(false);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        //get provider
        provider = mLocationManager.getBestProvider(criteria, true);
        
        //mLocationListener be use to listener location change
        mLocationManager.requestLocationUpdates(provider, 3000, 50, mLocationListener);
    }
    
    
    public Location getLastKnownLocation(){
        return mLocationManager.getLastKnownLocation(provider);
    }
    
    /**
     * location listener
     */
    private final LocationListener mLocationListener = new LocationListener() {
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.v(TAG, "onStatusChanged");
        }
        
        public void onProviderEnabled(String provider) {
            Log.v(TAG, "onProviderEnabled");
        }
        
        public void onProviderDisabled(String provider) {
            Log.v(TAG, "onProviderDisabled");
        }
        
        public void onLocationChanged(Location location) {
//            Log.v(TAG, "onLocationChanged");
            Log.v(TAG, "latitude: " + location.getLatitude() + ", longitude: " + location.getLongitude());
            
            updateLocation(location);
        }
    };
    
    /**
     * update longitude and latitude to server
     * @param location
     */
    private void updateLocation(final Location location) {
        new Thread(){
            public void run() {
                List<BasicNameValuePair> params = OAuth.createAuthParams();
                params.add(new BasicNameValuePair("longitude", Double.toString(location.getLongitude())));
                params.add(new BasicNameValuePair("latitude", Double.toString(location.getLatitude())));
                try {
                    Utils.openUrl(UPDATE_URL, params);
                } catch (NoHttpResponseException e) {
                    Log.w(TAG, e.getMessage());
                } catch (IOException e) {
                    Log.w(TAG, e.getMessage());
                }
            }
        }.start();
    }

}
